package com.cy.controller;

import com.cy.service.ICartService;
import com.cy.util.JsonResult;
import com.cy.vo.CartVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 时间：2023/1/7
 */
@RestController
@RequestMapping("/carts")
public class CartController extends BaseController{
    @Resource
    private ICartService cartService;

    @RequestMapping("/add_to_cart")
    public JsonResult<Void> addToCart(Integer pid, Integer amount, HttpSession httpSession){
        cartService.addToCart(getuidFromSession(httpSession),pid,amount,getUsernameFromSession(httpSession));
        return new JsonResult<>(OK);
    }

    @GetMapping({"", "/"})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session) {
        // 从Session中获取uid
        Integer uid = getuidFromSession(session);
        // 调用业务对象执行查询数据
        List<CartVO> data = cartService.getVOByUid(uid);
        // 返回成功与数据
        return new JsonResult<List<CartVO>>(OK, data);
    }

    @RequestMapping("/{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid")Integer cid, HttpSession httpSession){
        Integer data = cartService.addNum(cid,getuidFromSession(httpSession),getUsernameFromSession(httpSession));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/list")
    public JsonResult<List<CartVO>> getVOByCids(Integer[] cids,HttpSession httpSession){
        List<CartVO> data = cartService.getVOByCids(getuidFromSession(httpSession),cids);
        return new JsonResult<>(OK,data);
    }
}
